package com.kaibai.service.impl;

import com.kaibai.dao.CartDao;
import com.kaibai.dao.GoodDao;
import com.kaibai.dao.OrderDao;
import com.kaibai.dao.OrderItemDao;
import com.kaibai.dao.impl.CartDaoImpl;
import com.kaibai.dao.impl.GoodDaoImpl;
import com.kaibai.dao.impl.OrderDaoImpl;
import com.kaibai.dao.impl.OrderItemDaoImpl;
import com.kaibai.pojo.*;
import com.kaibai.service.OrderService;
import com.kaibai.utils.JDBCUtil;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private OrderItemDao orderItemDao = new OrderItemDaoImpl();
    private CartDao cartDao = new CartDaoImpl();
    GoodDao goodDao = new GoodDaoImpl();



    @Override
    public String createOrder(Cart cart, Integer userId) {
        Connection conn = null;
        Order order = null;
        String orderId = System.currentTimeMillis()+""+userId;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            order = new Order(orderId,new Date(),cart.getTotalPrice(),0,userId);
            orderDao.saveOrder(conn,order);

            for (Map.Entry<Integer, CartItem> entry : cart.getItem().entrySet()) {
                CartItem cartItem = entry.getValue();
                OrderItem orderItem = new OrderItem(null,cartItem.getName(),cartItem.getCount(),cartItem.getPrice(),cartItem.getTotalPrice(),orderId);
                orderItemDao.saveOrderItem(conn,orderItem);
                Good good = goodDao.getGoodById(conn,cartItem.getId());
                good.setSales(good.getSales() + cartItem.getCount());
                if(cartItem.getCount() > good.getStock()){
                    throw new Exception("");
                }
                good.setStock(good.getStock() - cartItem.getCount());
                goodDao.update(conn,good);
            }
            cart.clear();
            conn.commit();
        } catch (Exception throwables) {
            try {
                conn.rollback();
                return null;
            } catch (Exception e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return orderId;
    }

    @Override
    public List<Order> showMyOrders(Integer userid) {
        Connection conn = null;
        List<Order> list = null;
        try {
            conn = JDBCUtil.getConnection();
            list = orderDao.queryOrdersByUserId(conn,userid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return list;
    }

    @Override
    public List<OrderItem> showOrderDetail(String orderid) {
        Connection conn = null;
        List<OrderItem> list = null;
        try {
            conn = JDBCUtil.getConnection();
            list = orderItemDao.queryOrderItemsByOrderId(conn,orderid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return list;
    }



    @Override
    public List<Order> showAllOrders() {
        Connection conn = null;
        List<Order> list = null;
        try {
            conn = JDBCUtil.getConnection();
            list = orderDao.queryOrders(conn);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return list;
    }

    @Override
    public int sendOrder(String orderid) {
        Connection conn = null;
        int res = 0;
        try {
            conn = JDBCUtil.getConnection();
            res = orderDao.changeOrderStatus(conn,orderid,1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return res;
    }

    @Override
    public int receiverOrder(String orderid) {
        Connection conn = null;
        int res = 0;
        try {
            conn = JDBCUtil.getConnection();
            res = orderDao.changeOrderStatus(conn,orderid,2);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return res;
    }

    @Override
    public String createOrderDB(int uid,int totalPrice) {
        Connection conn = null;
        Order order = null;
        String orderId = System.currentTimeMillis()+""+uid;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            order = new Order(orderId,new Date(),new BigDecimal(totalPrice),0,uid);
            orderDao.saveOrder(conn,order);
            List<CartItemDB> list = cartDao.getListByUid(conn,uid);
            for(CartItemDB cartItemDB : list){
                OrderItem orderItem = new OrderItem(null,cartItemDB.getName(),cartItemDB.getCount(),cartItemDB.getPrice(),new BigDecimal(totalPrice),orderId);
                orderItemDao.saveOrderItem(conn,orderItem);
                Good good = goodDao.getGoodById(conn,cartItemDB.getgId());
                good.setSales(good.getSales() + cartItemDB.getCount());
                if(cartItemDB.getCount() > good.getStock()){
                    throw new Exception("");
                }
                good.setStock(good.getStock() - cartItemDB.getCount());
                goodDao.update(conn,good);
            }
            cartDao.deleteItemDBByUid(conn,uid);
            conn.commit();
        } catch (Exception throwables) {
            try {
                conn.rollback();
                return null;
            } catch (Exception e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return orderId;
    }


}
